<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像 -->
            <div class="avatar_box">
                <img src="../../assets/1.gif" alt="">
            </div>
            <!-- 登录表单区域 -->
            <el-form ref="loginFormRef" :rules="Loginrules" :model="loginForm" class="login_form" style="top:110px;">
                <!-- 用户名 -->
                <el-form-item prop="name">
                    <el-input v-model="loginForm.name" placeholder="用户名" prefix-icon="el-icon-user-solid"></el-input>
                </el-form-item>
                <!-- 密码 -->
                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" placeholder="密码" prefix-icon="el-icon-s-goods"></el-input>
                </el-form-item>
                   <!-- 确认密码 -->
                <el-form-item prop="passwordpass">
                    <el-input v-model="loginForm.passwordpass" placeholder="确认密码" prefix-icon="el-icon-s-goods"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                   <el-button type="primary" @click="login">返回登录</el-button>
                   <el-button type="success" @click="register">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import { setToken, setUserName } from "../../utils/auth";
import request from "../../utils/request";
export default{
  data(){
    return{
         loginForm: {
            name:'admin',
            password:'123456',
            passwordpass:'123456'
        },
         //表单的验证规则对象
            Loginrules:{
                // 验证用户名是否合法
                name:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 5, message: '用户名长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                // 验证密码是否合法
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 8, message: '密码长度在 3 到 8 个字符', trigger: 'blur' }
                ],
                  // 验证密码是否合法
                passwordpass: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 8, message: '密码长度在 3 到 8 个字符', trigger: 'blur' }
                ]
            }
    }
  },
  methods:{
    register(){
    //    alert('1');
        var id = 1;
        var name = this.loginForm.name;
        var password = this.loginForm.password;
       
        console.log(name)
        // var passwordpass = this.loginForm.password;
        // if(password !== passwordpass){
        //   this.$message.error("俩次密码输入不一致");
        // } 
        request({
            url: "/api/user/add",
            method: "post",
            data: {
            id,
            name,
            password
            }
        }).then(res => {
            console.log(111)
            if (res.msg === "新增成功") {
            this.$message({
                message: "恭喜你，注册用户成功",
                type: "success"
            });
            setToken(res.token);
            setUserName(res.userName);
            this.$router.push("/");
            }
        });
    },
    login(){
        this.$router.push('/login')
    }
  }
}
</script>  
<style lang="less" scoped>
.login_container{
    background:deepskyblue;
    height: 100%;
}
.login_box{
    width: 450px;
    height: 380px;
    background-color:#fff;
    border-radius:3px;
    position:absolute;
    left:50%;
    top:50%;
    transform:translate(-50%,-50%);
    .avatar_box{
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        border-radius:50%;
        padding:10px;
        box-shadow:0 0  10px #ddd;
        position:absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width:100%;
            height: 100%;
            border-radius: 50%;
            background-color:#eee;
        }
    }
}
.login_form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
.btns{
    display: flex;
    justify-content:center;
}
</style>